#include <stdio.h>

int main(int argc, char const *argv[])
{
    FILE *fp = fopen("./14_test.txt","w");
    if( NULL == fp )
    {
        printf("Fail to open file\n");
        return -1;
    }
 
    long lLength;
    int ret;
    
    ret = fseek(fp,10, SEEK_SET);
    printf("fseek ret = %d\n", ret);
    fputc('A', fp);
    lLength = ftell(fp);
    printf("Current Length is %ld\n", lLength); //11

    fputc('B', fp);
    lLength = ftell(fp);
    printf("Current Length is %ld\n", lLength); //12

    fseek(fp, -4, SEEK_CUR);  
    fputc('C', fp);           // od -c ./14_test.txt
    lLength = ftell(fp);
    printf("Current Length is %ld\n", lLength); //9

    fseek(fp, 3, SEEK_END);
    fputc('D', fp);           // od -c ./14_test.txt
    lLength = ftell(fp);
    printf("Current Length is %ld\n", lLength); //16

    ret = fseek(fp, -2, SEEK_END);
    printf("fseek ret = %d\n", ret);
    fputc('E', fp);           // od -c ./14_test.txt
    lLength = ftell(fp);
    printf("Current Length is %ld\n", lLength); //15

    fseek(fp, 0, SEEK_END);
    lLength = ftell(fp);
    printf("Total File Length is %ld\n", lLength); //16

    rewind(fp);
    fputc('F', fp);           // od -c ./14_test.txt

    fclose(fp);

    return 0;
}

/*
在打开的文件流中, 保存着一个全局指针变量, 指向文件读写的下一个操作位置
当读写操作函数完成读写操作之后, 会把指针指向下一个位置.

函数 fseek
    可以设置当前操作位置

在过程中, 可以用 od -c 命令来实时查看 fseek 的执行结果

*/